package com.yuw.model.userinfo;

import com.yuw.bean.userinfo.UserInfoBean;
import com.yuw.dao.DBProvider;
import com.yuw.utils.MyUtils;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

public class ModelUserInfo {

    /**
     * 通过参数查询用户信息
     *
     * @param userInfoBean 参数实体类
     * @return 查询结果集
     */
    public List<UserInfoBean> queryUserinfoByParams(UserInfoBean userInfoBean) {
        // 默认返回值
        List<UserInfoBean> lstUsers = null;
        // 数据库查询操作sql
        //String strSql = " SELECT * FROM userinfo ";
        /*
          SELECT * FROM userinfo where userIsDelete = 0 and username like '%ad%' and userTel like '%150%'  and userAddress like '%山西%' and userBirthday > '2021-06-01' and userBirthday <'2021-06-04' and userRegDate > '2021-05-01' and userRegDate < '2021-06-04'
         */

        // sql语句动态拼写
        StringBuilder stringBuilder = new StringBuilder(" SELECT * FROM userinfo where userIsDelete = 0 ");
        // sql语句的参数动态集成 list
        List<Object> lstParams = new ArrayList<>();
        // 根据参数实体类的查询条件的属性值不为空或null，则将该属性值作为查询条件进行查询
        if (userInfoBean != null) {

            if (StringUtils.isNotEmpty(userInfoBean.getUserName())) {
                stringBuilder.append(" and username like ? ");
                // 保存对应的参数
                lstParams.add("%" + userInfoBean.getUserName() + "%");
            }

            if (StringUtils.isNotEmpty(userInfoBean.getUserTel())) {
                stringBuilder.append(" and userTel like ? ");
                // 保存对应的参数
                lstParams.add("%" + userInfoBean.getUserTel() + "%");
            }

            if (StringUtils.isNotEmpty(userInfoBean.getUserAddress())) {
                stringBuilder.append(" and userAddress like ? ");
                // 保存对应的参数
                lstParams.add("%" + userInfoBean.getUserAddress() + "%");
            }

            if (userInfoBean.getUserBirthday() != null) {
                stringBuilder.append(" and userBirthday >= ? ");
                // 保存对应的参数
                lstParams.add(userInfoBean.getUserBirthday());
            }

            if (userInfoBean.getUserBirthday2() != null) {
                stringBuilder.append(" and userBirthday <= ? ");
                // 保存对应的参数
                lstParams.add(userInfoBean.getUserBirthday2());
            }

            if (userInfoBean.getUserRegDate() != null) {
                stringBuilder.append(" and userRegDate >= ? ");
                // 保存对应的参数
                lstParams.add(userInfoBean.getUserRegDate());
            }

            if (userInfoBean.getUserRegDate2() != null) {
                stringBuilder.append(" and userRegDate <= ? ");
                // 保存对应的参数
                lstParams.add(userInfoBean.getUserRegDate2());
            }
        }

        // 输出sql语句和参数信息
        System.out.println("查询SQL：" + stringBuilder.toString());
        System.out.println("查询参数：" + lstParams);

        // TODO 动态查询语句的拼接处理
        // 调用dao层进行数据库操作
        lstUsers = MyUtils.getNewInstance(DBProvider.class)
                .query(stringBuilder.toString(), new BeanListHandler<>(UserInfoBean.class), lstParams.toArray());
        // 返回值
        return lstUsers;
    }

    /**
     * 登录业务逻辑处理
     *
     * @param userInfoBean 参数实体类
     * @return 登陆验证结果
     */
    public boolean doLogin(UserInfoBean userInfoBean) {
        // 登录业务逻辑验证处理
        // 登录查询的sql语句
        String strSql = "SELECT * FROM userinfo WHERE userName = ? AND userPsw = ?";
        // 调用dao层进行数据库操作
        List<UserInfoBean> lstUsers = MyUtils.getNewInstance(DBProvider.class).
                query(strSql, new BeanListHandler<>(UserInfoBean.class), new Object[]{userInfoBean.getUserName(), userInfoBean.getUserPsw()});
        // 返回结果
        return lstUsers != null && lstUsers.size() > 0;
    }
}
